﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NBlock.MVCExtensions;
using NBlock.Core.ORM;
using NBlock.Core;
using System.Linq.Expressions;

namespace Cost.Models
{
    public class CommonLogic
    {
        /// <summary>
        /// 获取树状的组织机构
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="orgs"></param>
        /// <returns></returns>
        public static List<nbJsonTreeNode> GetSubOrgTree(string parentId, IList<Organization> orgs)
        {
            parentId = parentId ?? string.Empty;
            var list = new List<nbJsonTreeNode>();
            foreach (var org in orgs.Where(o => o.ParentId == parentId))
            {
                var Node = new nbJsonTreeNode
                {
                    id = org.Id,
                    text = org.OrgName,
                    value = org.OrgCode,
                    isexpand = org.FullPath.Split(ModelConstant.FULL_PATH_SEPARATOR.ToCharArray()).Length <= 1,
                };
                Node.ChildNodes = GetSubOrgTree(org.Id, orgs);
                list.Add(Node);
            }

            return list;
        }

        /// <summary>
        /// 生成子树
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="privileges"></param>
        /// <returns></returns>
        public static List<nbJsonTreeNode> GetSubPrivilegeTree(string parentId, IList<Privilege> privileges, IList<Permission> permissions = null)
        {
            parentId = parentId ?? string.Empty;
            var list = new List<nbJsonTreeNode>();
            foreach (var p in privileges.Where(o => o.ParentId == parentId))
            {
                var Node = new nbJsonTreeNode();
                Node.id = p.Id;
                Node.text = p.PrivilegeName;
                Node.value = p.Id;
                Node.isexpand = true;
                Node.complete = true;
                if (permissions != null)
                {
                    Node.showcheck = true;
                    Node.checkstate = GetCheckState(p.Id, privileges, permissions);
                }
                Node.ChildNodes = GetSubPrivilegeTree(p.Id, privileges, permissions);
                list.Add(Node);
            }

            return list;
        }
        private static byte GetCheckState(string privilegeId, IList<Privilege> privileges, IList<Permission> permissions)
        {
            if (permissions != null)
            {
                // 选中状态
                if (permissions.Count(a => a.PrivilegeId == privilegeId) > 0)
                {
                    return (byte)1;
                }
                // 半选中状态，只要有一个子节点
                if (permissions.Count(p => privileges.FirstOrDefault(b => b.Id == p.PrivilegeId) != null
                        && privileges.FirstOrDefault(b => b.Id == p.PrivilegeId).FullPath.Contains(privilegeId)) > 0)
                {
                    return (byte)2;
                }
            }

            return (byte)0;
        }

        /// <summary>
        /// 获取下周一的日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetNextMonday()
        {
            var dayOfWeek = (double)DateTime.Today.DayOfWeek;
            dayOfWeek = dayOfWeek <= 0 ? 7 : dayOfWeek;
            return DateTime.Today.AddDays(8 - dayOfWeek);
        }
    }
}